import { createRouter, createWebHistory } from "vue-router";
import { useUserStore } from "../stores/UserStore";
import LoginVue from "../views/LoginVue.vue";
import HomeVue from "../views/HomeVue.vue";
import InvitationPage from "../components/Tenant/InvitationPage.vue";
const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    {
      path: "/login",
      component: LoginVue,
      name: "Login",
    },
    {
      path: "/invite/accept",
      name: "InvitationAccept",
      component: InvitationPage,
      props: (route) => ({ token: route.query.token }),// 通过 props 将 Token 传递给组件。
    },
    {
      path: "/home",
      component: HomeVue,
      name: "HomeVue",
      meta: { requiresAuth: true }, // 需要认证
      children: [
        {
          path: "tenant/user",
          name: "UserManagement",
          component: () => import("../views/Tenant/UserManagement.vue"),
          meta: { roles: ["ROLE_admin", "ROLE_super"] }, // 仅管理员可访问
        },
        {
          path: "tenant/manage",
          name: "TenantManagement",
          component: () => import("../views/Tenant/TenantManagement.vue"),
          meta: { roles: ["ROLE_admin", "ROLE_super"] }, // 仅管理员可访问
        },
        {
          path: "tenant/project", //前面不要加斜杠
          name: "ProjectManagement",
          component: () => import("../views/Tenant/ProjectManagement.vue"),
          meta: { roles: ["ROLE_admin", "ROLE_super"] }, // 管理员可访问
        },
        {
          path: "tenant/task", //前面不要加斜杠
          name: "TaskManagement",
          component: () => import("../views/Tenant/TaskManagement.vue"),
          meta: { roles: ["ROLE_admin", "ROLE_super"] }, // 仅管理员可访问
        },
        {
          path: "tenant/notice", //前面不要加斜杠
          name: "NoticeManagement",
          component: () => import("../views/Tenant/Notice.vue"),
          meta: { roles: ["ROLE_admin", "ROLE_super", "ROLE_user"] },
        },
        {
          path: "cost/basicInfo",
          name: "CostBasicInfo",
          component: () => import("../views/Cost/CostBasicInfo.vue"),
          meta: { roles: ["ROLE_appraiser", "ROLE_super"] }, //评估师可访问
        },
        {
          path: "cost/calculation",
          name: "CostCalculation",
          component: () => import("../views/Cost/CostCalculation.vue"),
          meta: { roles: ["ROLE_appraiser", "ROLE_super"] }, // 评估师可访问
        },
        {
          path: "report/generate",
          name: "ReportGenerate",
          component: () => import("../views/Report/ReportGenerate.vue"),
          meta: { roles: ["ROLE_auditor", "ROLE_super"] }, // 评估师可访问
        },
        {
          path: "report/review",
          name: "ReportReview",
          component: () => import("../views/Report/ReportReview.vue"),
          meta: { roles: ["ROLE_auditor", "ROLE_super"] }, // 审核员可访问
        },
        {
          path: "report/Kanban",
          name: "ReportKanban",
          component: () => import("../views/Report/ReportKanban.vue"),
          meta: { roles: ["ROLE_admin", "ROLE_super"] }, // 管理员可访问
        },
      ],
    },
  ],
});
// 导航守卫
router.beforeEach((to, from, next) => {
  //这三个参数分别是即将要进入的路由对象、当前导航正要离开的路由、执行下一个导航钩子函数
  const userStore = useUserStore();

  // 检查是否需要认证
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    if (!userStore.userInfo) {
      // 未登录，重定向到登录页
      next({ name: "Login" });
    } else {
      // 检查是否有权限访问该路由
      const roles = to.meta.roles as string[]; // 将 roles 强制转换为 string[]
      const premissioms = userStore.userInfo.authorityStrings || [];
      if (roles && roles.length > 0) {
        const hasPermission = roles?.some(
          (role) => premissioms.indexOf(role) >= 0
        );
        if (hasPermission) {
          next();
        } else {
          // 无权限，重定向到无权限页面
          next({ name: "HomeVue" });
        }
      } else {
        // 不需要特定角色，允许访问
        next();
      }
    }
  } else {
    // 不需要认证的路由，允许访问
    next();
  }
});
export default router;
